package com.spider.sql.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestBody Map<String, String> loginData) {
        String username = loginData.get("username");
        String password = loginData.get("password");

        // 假定账号密码是 "admin" 和 "123456"
        if ("admin".equals(username) && "123456".equals(password)) {
            return "success";
        } else {
            return "fail";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        System.out.println("退出登录");
        session.removeAttribute("spiderData");
        session.removeAttribute("sqlData");
        // 清空Session
        session.invalidate();
        // 重定向到登录页面
        return "redirect:/";
    }


    @RequestMapping("/successful")
    public String login() {
        return "welcome";
    }

}
